package com.gz.purchase.library.listener;

import com.gz.purchase.library.pojo.StorageCheckProductInfo;
import com.gz.purchase.library.util.Constants;
import com.gz.purchase.library.util.CrashHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 入库界面查询 商品的监听器
 * author wanner1991
 */
public abstract class SerarchProductListener extends BaseListener{
    private String TAG = SerarchProductListener.class.getSimpleName();
    @Override
    public void onNoMoreData() {

    }

    @Override
    public void onOffline() {

    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        try {
            int status = response.getInt(Constants.REQ_STATUS);
            String msg = response.getString(Constants.REQ_MSG);
            CrashHandler.i(TAG, "入库界面查询 商品status = " + status);
            CrashHandler.i(TAG, "入库界面查询 商品msg = " + msg);
            switch(status){
                case Constants.REQ_OK:
                    CrashHandler.i(TAG,response.toString());
                    onResponseSuccess(StorageCheckProductInfo.parse3(response.getJSONArray(Constants.REQ_RESULT)));
                    break;
                case Constants.REQ_SEARCH_PRODUCT_NO_PRODUCT:
                    onSatisfiedProducts();
                    break;
                default:
                    onResponseFailed();
            }

        } catch (JSONException e) {
            onFailure(statusCode, headers, e, new JSONObject());
        }
    }
    public abstract void onSatisfiedProducts();
    public abstract void onResponseSuccess(List<StorageCheckProductInfo> list);
    @Override
    public void onFailure(int statusCode, Header[] headers,
                          Throwable throwable, JSONObject errorResponse) {
        CrashHandler.e(throwable);
        onResponseFailed();
    }
}
